//## 회계(출금) 저장 ####################
function fnSave()
{
	if (!$("#article").val())
	{
		alert("내역을 바르게 입력해 주세요!");
		$("#article").focus();
		return;
	}
	if (!$("#accountCd").val())
	{
		alert("계정을 선택해 해주세요!");
		$("#accountCd").focus();
		return;
	}
	if (!$("#amount").val())
	{
		alert("금액을 바르게 입력해 주세요!");
		$("#amount").focus();
		return;
	}
	if (!$("#planDt").val())
	{
		alert("예정일을 바르게 입력해 주세요!");
		$("#planDt").focus();
		return;
	}
	if (!$("#orgAmount").val())
	{
		alert("원금을 바르게 입력해 주세요!");
		$("#orgAmount").focus();
		return;
	}

	var valUrl = '/action/accountOut/save.asp';
	var param  = "article="+encodeURIComponent($("#article").val());
		param += "&accountCd="+$("#accountCd").val();
		param += "&flag="+$("#flag").val();
		param += "&amount="+$("#amount").val();
		param += "&orgAmount="+$("#orgAmount").val();
		param += "&tax="+$("#tax").val();
		param += "&planDt="+$("#planDt").val();
		param += "&compCd="+$("#compCd").val();
		param += "&payAmount="+$("#payAmount").val();
		param += "&actionDt="+$("#actionDt").val();
		param += "&bankDt="+$("#bankDt").val();
		param += "&memo="+encodeURIComponent($("#memo").val());
		param += "&payFlag="+$("#payFlag").val();
		param += "&payCd="+$("#payCd").val();
		param += "&seq="+$("#seq").val();
		param += "&part="+$("#part").val();
		param += "&payMethod="+$("#payMethod").val();

	if (xMode=="Debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			fnSessionCheck($(xml).find("MSG").text());
			if ($(xml).find("MSG").text() == "Save OK!")
			{
				alert("Save OK!");
				fnReset();
				grid01List();
			}
			else alert($(xml).find("MSG").text());
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 회계(출금) 그리드 표시 ####################

var G_curPage01 = 1;

function grid01List(page,accSum)
{	
	var gridNo = 1;
	var xmlNode = "PayList";

	fnResetGrid(gridNo);
	fnNewLoading(gridNo);

	if (!page) page = G_curPage01;
	if (page == undefined) page = 1;
	else G_curPage01 = page;
	var limit = 50;

	var valUrl = "/action/accountOut/list.asp";

	var Parm = "findArticle="+encodeURIComponent($("#findArticle").val());
		Parm += "&findAccountCd="+$("#findAccountCd").val();
		Parm += "&findCompCd="+$("#findCompCd").val();
		Parm += "&findPayMethod="+$("#findPayMethod").val();
		Parm += "&findMnth="+$("#findMnth").val();
		Parm += "&findPart="+$("#findPart").val();
		Parm += "&stDate="+$("#stDate").val();
		Parm += "&endDate="+$("#endDate").val();
		Parm += "&keyset="+$("#keyset").val();

	if (xMode=="Debug") window.open(valUrl+'?xMode=debug&'+Parm);

	// 그리드 헤더를 기준으로 Cell의 폭을 구해온다.
	var cellWidth = fnGetGridCellWidth(gridNo);	
	// Cell의 정렬방식
	var cellAlign = ",,,,left,,right,right,,right".split(",");
	// Cell의 링크 걸지 여부 결정
	var cellLink = ",,,,,,,,,,".split(",");

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: Parm,
		success: function(xml){
			var gridList = "";
			var k = 1;
			var ttlCnt = $(xml).find(xmlNode).length;
			if (ttlCnt%limit == 0) var totalPage = parseInt((ttlCnt / limit),0);
			else 	var totalPage = parseInt((ttlCnt / limit),0)  + 1;
			var cPageNo = (totalPage) * limit;
			if (cPageNo > ttlCnt) cPageNo = ttlCnt;
			
			var startNo = (parseInt(page,10)-1) * limit;
			var endNo = parseInt(page,10) * limit;
			var bgColor = "#FFF";
			var chkCnt = 0;
			
			// 그리드의 부가 정보(레코드수, 페이징) 표시 또는 가리기
			fnGridExtraInfo(gridNo, ttlCnt, page, limit); 

			if (ttlCnt <= endNo) endNo = ttlCnt;
			var no = endNo;
			var ttlAmt = 0;
			var ttlPayAmt = 0;
			var ttlOrgAmt = 0;
			var oPayCd = "";
			var empty = "&nbsp;";
			
			$(xml).find(xmlNode).each(function(idx){

				no = cPageNo;
				
				if (k>startNo && k<=endNo)
				{
					var payCd			= $(this).find("payCd").text();
					var seq				= $(this).find("seq").text();
					var accountNm	= $(this).find("accountNm").text();
					var article			= $(this).find("article").text();
					var company		= $(this).find("company").text();
					var amount			= $(this).find("amount").text();
					var orgAmount	= $(this).find("orgAmount").text();
					var tax					= $(this).find("tax").text();
					var payAmount	= $(this).find("payAmount").text();
					var planDt			= $(this).find("planDt").text();
					var actionDt		= $(this).find("actionDt").text();
					var bankDt			= $(this).find("bankDt").text();
					var payStatus		= $(this).find("status").text();
					var partNm			= $(this).find("partNm").text();
					var payMemo			= $(this).find("memo").text();

					if(payMemo) { var memoFlag = "[M]"; var style ="color:#cc0000;font-size:8pt;"; }
					else { var memoFlag = ""; var style =""; }
							
					if (oPayCd == payCd)
					{
						accountNm = "-";
						company = "-";
						article = "-";
						amount = "";
					}

					tax = "<br>"+tax;

					var arrVal = new Array();
					arrVal[0] = "<p>"+no+"</p>";
					arrVal[1] = "<p>"+planDt+"</p>";
					arrVal[2] = "<p>"+partNm+"</p>";
					arrVal[3] = "<p>"+accountNm+"</p>";
					arrVal[4] = "<p>"+article+"</p>";
					arrVal[5] = "<p>"+company+"&nbsp;</p>";
					arrVal[6] = "<p>"+amount+"</p>";
					arrVal[7] = "<p style=\"margin-top:2px;\">"+orgAmount+tax+"</p>";
					arrVal[8] = "<p>"+actionDt+"</p>";
					arrVal[9] = "<p>"+memoFlag+"&nbsp;"+payAmount+"</p>";
					arrVal[10] = "<p>"+bankDt+"</p>";
					
					if (amount!="") ttlAmt += parseInt(amount.split(",").join(""),10);
					if (payAmount!=0) ttlPayAmt += parseInt(payAmount.split(",").join(""),10);
					if (orgAmount!=0) ttlOrgAmt += parseInt(orgAmount.split(",").join(""),10);
					
					if (payStatus == "Y") bgColor="#F2F2F2";
					else bgColor="#FFF";
					var link = "fnSetPay('"+payCd+"','"+seq+"')";

					//gridList = fnDrawCell(lotYear, lotNo, lotSubNo, processCd);
					gridList = fnDrawCell(arrVal, bgColor, cellWidth, cellAlign, '', link, cellLink);

					$("#"+"gridBody"+gridNo).append(gridList);
				}
				cPageNo--;
				oPayCd = payCd;
				k++;			
			});
			var arrVal = new Array();
			arrVal[0] = "<p>"+empty+"</p>";
			arrVal[1] = "<p><b>합 계</b></p>";
			arrVal[2] = "<p>"+empty+"</p>";
			arrVal[3] = "<p>"+empty+"</p>";
			arrVal[4] = "<p>"+empty+"</p>";
			arrVal[5] = "<p>"+empty+"&nbsp;"+"</p>";
			arrVal[6] = "<p><b>"+getNumWithComma(ttlAmt)+"</b></p>";
			arrVal[7] = "<p><b>"+getNumWithComma(ttlOrgAmt)+"</b></p>";
			arrVal[8] = "<p>"+empty+"</p>";
			arrVal[9] = "<p><b>"+getNumWithComma(ttlPayAmt)+"</b></p>";
			arrVal[10] = "<p>"+empty+"</p>";
			bgColor="#f0faff";
			gridList = fnDrawCell(arrVal, bgColor, cellWidth, cellAlign, '', '', cellLink);
			$("#"+"gridBody"+gridNo).prepend(gridList);
			$("#"+"gridBody"+gridNo).append(gridList);
		},
		error: function(xml){
			alert('Error!!\r\n'+xml.responseText);
		}
	});
	if (accSum == undefined) setTimeout("grid03List(1);",500);
}


//## 회계(출금) 계정별 합계 그리드 표시 ####################

var G_curPage02 = 1;

function grid02List(page)
{	
	$("#layerSum1").css("display","none");
	$("#layerSum2").css("display","");
	
	var gridNo = 2;
	var xmlNode = "PaySumList";

	fnResetGrid(gridNo);
	fnNewLoading(gridNo);

	if (!page) page = G_curPage02;
	if (page == undefined) page = 1;
	else G_curPage02 = page;
	var limit = 50;

	var valUrl = "/action/accountOut/listAccountSum.asp";

	var	Parm = "keyset="+$("#keyset").val();
		Parm += "&stDate="+$("#stDate").val();
		Parm += "&endDate="+$("#endDate").val();
		Parm += "&findMnth="+$("#findMnth").val();
		Parm += "&findPayMethod="+$("#findPayMethod").val();
		Parm += "&findPart="+$("#findPart").val();
		Parm += "&findArticle="+encodeURIComponent($("#findArticle").val());
		Parm += "&findAccountCd="+$("#findAccountCd").val();
		Parm += "&findCompCd="+$("#findCompCd").val();
		
	if (xMode=="Debug") window.open(valUrl+'?xMode=debug&'+Parm);

	// 그리드 헤더를 기준으로 Cell의 폭을 구해온다.
	var cellWidth = fnGetGridCellWidth(gridNo);	
	// Cell의 정렬방식
	var cellAlign = ",left,right,right,right".split(",");
	// Cell의 링크 걸지 여부 결정
	var cellLink = ",,,,,".split(",");

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: Parm,
		success: function(xml){
			var gridList = "";
			var k = 1;
			var ttlCnt = $(xml).find(xmlNode).length;
			var startNo = (parseInt(page,10)-1) * limit;
			var endNo = parseInt(page,10) * limit;
			var bgColor = "#FFF";
			var chkCnt = 0;
			
			// 그리드의 부가 정보(레코드수, 페이징) 표시 또는 가리기
			fnGridExtraInfo(gridNo, ttlCnt, page, limit); 

			if (ttlCnt <= endNo) endNo = ttlCnt;
			var no = endNo;
			var empty = "&nbsp;";
			var ttlAmt = 0;
			var ttlPayAmt = 0;
			$(xml).find(xmlNode).each(function(idx){

				if (k>startNo && k<=endNo)
				{
					var accountNm	= $(this).find("accountNm").text();
					var accountCd	= $(this).find("accountCd").text();
					var amount		= $(this).find("amount").text();
					var ttlRate		= $(this).find("ttlRate").text();
					var orgAmount	= $(this).find("orgAmount").text();
					var tax			= $(this).find("taxAmount").text();
					var payAmount	= $(this).find("payAmount").text();
					var notPayAmt	= $(this).find("notPayAmt").text();

					var style = "";
					if (accountNm.indexOf("TOTAL")>0) { no = "&nbsp;"; bgColor="#f0faff"; style="font-weight:bold;"}
					else accountNm = "["+ttlRate+"%] "+accountNm;
					
					ttlAmt = amount;
					ttlPayAmt = payAmount;
					
					if (no > 1) 	var vNo = no -1;
					else var vNo = "&nbsp;";
					
					var arrVal = new Array();
					arrVal[0] = "<p>"+vNo+"</p>";
					arrVal[1] = "<p style='"+style+"'>"+accountNm+"</p>";
					if (accountNm.indexOf("TOTAL")>0) arrVal[2] = "<p style='margin-top:2px;"+style+"'>"+amount+"<br><font style='font-weight:normal'>"+orgAmount+"</font></p>";
					else arrVal[2] = "<p style='"+style+"'>"+amount+"</p>";
					arrVal[3] = "<p style='"+style+"'>"+payAmount+"</p>";
					arrVal[4] = "<p style='color:#FF0000;"+style+"'>"+notPayAmt+"</p>";
															
					var link = "fnAccList('"+accountCd+"')";
					gridList = fnDrawCell(arrVal, bgColor, cellWidth, cellAlign, '', link, cellLink);

					$("#"+"gridBody"+gridNo).append(gridList);
				}
				no = parseInt(endNo,10)-parseInt(k,10);
				k++;			
			});		
		},
		error: function(xml){
			alert('Error!!\r\n'+xml.responseText);
		}
	});

}

//## 회계(출금) 회사별 합계 그리드 표시 ####################
var G_curPage03 = 1;

function grid03List(page)
{	
	$("#layerSum1").css("display","");
	$("#layerSum2").css("display","none");
	
	var gridNo = 3;
	var xmlNode = "PaySumList";

	fnResetGrid(gridNo);
	fnNewLoading(gridNo);

	if (!page) page = G_curPage03;
	if (page == undefined) page = 1;
	else G_curPage03 = page;
	var limit = 100;

	var valUrl = "/action/accountOut/listCompanySum.asp";

	var	Parm = "keyset="+$("#keyset").val();
		Parm += "&stDate="+$("#stDate").val();
		Parm += "&endDate="+$("#endDate").val();
		Parm += "&findMnth="+$("#findMnth").val();
		Parm += "&findPayMethod="+$("#findPayMethod").val();
		Parm += "&findPart="+$("#findPart").val();
		Parm += "&findArticle="+encodeURIComponent($("#findArticle").val());
		Parm += "&findAccountCd="+$("#findAccountCd").val();
		Parm += "&findCompCd="+$("#findCompCd").val();
		
	if (xMode=="Debug") window.open(valUrl+'?xMode=debug&'+Parm);

	// 그리드 헤더를 기준으로 Cell의 폭을 구해온다.
	var cellWidth = fnGetGridCellWidth(gridNo);	
	// Cell의 정렬방식
	var cellAlign = ",left,right,right,right".split(",");
	// Cell의 링크 걸지 여부 결정
	var cellLink = ",,,,,".split(",");

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: Parm,
		success: function(xml){
			var gridList = "";
			var k = 1;
			var ttlCnt = $(xml).find(xmlNode).length;
			var startNo = (parseInt(page,10)-1) * limit;
			var endNo = parseInt(page,10) * limit;
			var bgColor = "#FFF";
			var chkCnt = 0;
			
			// 그리드의 부가 정보(레코드수, 페이징) 표시 또는 가리기
			fnGridExtraInfo(gridNo, ttlCnt, page, limit); 

			if (ttlCnt <= endNo) endNo = ttlCnt;
			var no = endNo;
			var empty = "&nbsp;";
			var ttlAmt = 0;
			var ttlPayAmt = 0;
			$(xml).find(xmlNode).each(function(idx){

				if (k>startNo && k<=endNo)
				{
					var compCd	= $(this).find("compCd").text();
					var company	= $(this).find("company").text();
					var amount		= $(this).find("amount").text();
					var orgAmount	= $(this).find("orgAmount").text();
					var tax			= $(this).find("taxAmount").text();
					var payAmount	= $(this).find("payAmount").text();
					var notPayAmt	= $(this).find("notPayAmt").text();

					var style = "";
					if (company.indexOf("TOTAL")>0) { no = "&nbsp;"; bgColor="#f0faff"; style="font-weight:bold;"}
					
					ttlAmt = amount;
					ttlPayAmt = payAmount;
					
					if (no > 1) 	var vNo = no -1;
					else var vNo = "&nbsp;";
					
					var arrVal = new Array();
					arrVal[0] = "<p>"+vNo+"</p>";
					arrVal[1] = "<p style='"+style+"'>"+company+"</p>";
					if (company.indexOf("TOTAL")>0) arrVal[2] = "<p style='margin-top:2px;"+style+"'>"+amount+"<br><font style='font-weight:normal'>"+orgAmount+"</font></p>";
					else arrVal[2] = "<p style='"+style+"'>"+amount+"</p>";
					arrVal[3] = "<p style='"+style+"'>"+payAmount+"</p>";
					arrVal[4] = "<p style='color:#FF0000;"+style+"'>"+notPayAmt+"</p>";
					//arrVal[4] = "<p style=\"margin-top:2px;\">"+orgAmount+"<br>"+tax+"</p>";
															
					var link = "fnCompList('"+company+"','"+compCd+"')";
					gridList = fnDrawCell(arrVal, bgColor, cellWidth, cellAlign, '', link, cellLink);

					$("#"+"gridBody"+gridNo).append(gridList);
				}
				no = parseInt(endNo,10)-parseInt(k,10);
				k++;			
			});		
		},
		error: function(xml){
			alert('Error!!\r\n'+xml.responseText);
		}
	});

}

//## 회사별 합계 검색
function fnCompList(company, compCd)
{
	fnSearchReset();
	fnFindCompany(company,compCd,'findCompCd');
	setTimeout("grid01List(1,'Company');",500);
}

//## 계정별 합계 검색
function fnAccList(accountCd)
{
	fnSearchReset();
	$("#findAccountCd").val(accountCd);
	grid01List(1,'Account');
}

//## 회계(출금) 표시 ####################
function fnSetPay(payCd, seq)
{
	var valUrl = "/action/accountOut/view.asp";
	var param = "payCd="+payCd;
		param += "&seq="+seq;

	if (xMode=="Debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());
			fnReset();

			$("#payCd").val($(xml).find("payCd").text());
			$("#seq").val($(xml).find("seq").text());
			$("#accountCd").val($(xml).find("accountCd").text());
			$("#article").val($(xml).find("article").text());
			$("#part").val($(xml).find("part").text());
			$("#payMethod").val($(xml).find("payMethod").text());
			$("#amount").val($(xml).find("amount").text());
			$("#orgAmount").val($(xml).find("orgAmount").text());
			$("#tax").val($(xml).find("tax").text());
			$("#payAmount").val($(xml).find("payAmount").text());
			$("#planDt").val($(xml).find("planDt").text());
			$("#actionDt").val($(xml).find("actionDt").text());
			$("#bankDt").val($(xml).find("bankDt").text());
			$("#flag").val($(xml).find("flag").text());
			$("#payFlag").val($(xml).find("payFlag").text());
			$("#memo").val($(xml).find("memo").text());
			//$("#accessInfo").val("최종수정 : "+$(xml).find("modDt").text());
			fnFindCompany('',$(xml).find("compCd").text(),'compCd');
			
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 회계(출금) Reset 기능
function fnReset()
{
	
	//$("#planDt").val();
	$("#amount").val("");
	$("#compCd").val("");
	$("#company").val("");
	$("#payCd").val("");
	$("#accountCd").val("");
	$("#article").val("");
	$("#amount").val("");
	$("#orgAmount").val("");
	$("#tax").val("");
	$("#flag").val("Y");
	fnPayResultReset();
}

function fnPayResultReset()
{
	$("#seq").val("");
	$("#payAmount").val("");
	$("#memo").val("");
	$("#payFlag").val("Y");
}

function fnResetTerms(f)
{
	if (f=="t") 
	{
		$("#stDate").val("");
		$("#endDate").val("");		
	}
	else $("#findMnth").val("");		
}

//## 검색 Reset 기능
function fnSearchReset()
{
	$("#findArticle").val("");
	$("#findAccountCd").val("");
	$("#findComapny").val("");
	$("#findPart").val("");
	$("#findPayMethod").val("");
	$("#findCompCd").val("");
	$("#keyset").val("planDt");
}

//## 지급액 동기화
function fnSetSame()
{
	$("#payAmount").val($("#amount").val());
}
//## 지급일 동기화
function fnSetSameDt()
{
	$("#bankDt").val($("#planDt").val());
	$("#actionDt").val($("#planDt").val());
}

//## 세금 계산
function fnSetTax()
{
	var amount = $("#amount").val();
	amount = amount.split(",").join("");
	if (amount > 0)
	{
		var tax = 0;
		tax = Math.round(parseInt(amount,10)/11);
		var orgAmount = parseInt(amount,10) - parseInt(tax,10);
		orgAmount = getNumWithComma(orgAmount);
		tax = getNumWithComma(tax);


		$("#tax").val(tax);
		$("#orgAmount").val(orgAmount);
	}
}